/**
 * @author: 子龙
 */
package com.fzl.lotterysystem.service.activitystatus.operator;

import com.fzl.lotterysystem.dao.dataobject.ActivityDO;
import com.fzl.lotterysystem.dao.mapper.ActivityMapper;
import com.fzl.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.fzl.lotterysystem.service.DTO.ConvertActivityStatusDTO;
import com.fzl.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.fzl.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum statusEnum=convertActivityStatusDTO.getTargetActivityStatus();

        if(activityId==null || statusEnum==null){
            return false;
        }
        ActivityDO activityDO= activityMapper.selectById(activityId);
        if(activityDO==null){
            return false;
        }
        //当前活动状态与传入的状态是否一致，一致就不处理
        if (statusEnum.name().equalsIgnoreCase(activityDO.getStatus())) {
            return false;
        }

        //判断奖品是否抽完
        //查询INIT个数的奖品
        int count= activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if(count>0){
            //奖品没有抽完
            return false;
        }

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        }catch (Exception e){
            return false;
        }

    }
}